// pages/traincar/orderdetail.js
var util = require('../../utils/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerUrl: '',
        keepItems:[],
        orderid: 0,
        orderinfo: [],
        isshow: true,
        keepmoney:0,
        othermoney:0,
        allmoney:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            orderid: options.oid
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getOrderdetail();
        this.initBanner();
    },
    //广告图片初始化
    initBanner: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'common/banner';
        var imgbanner = wx.getStorageSync('keepBanner');
        //console.log(imgbanner.length);
        if (!app.expiratTime('keepBanner') || imgbanner.length == 0) {
            wx.request({
                url: requrl,
                data: { 'location': '302' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        wx.setStorageSync('keepBanner', res.data.result[0].imgpath);
                        that.setData({
                            bannerUrl: res.data.result[0].imgpath
                        })
                    }
                }
            })
        }
        else {
            that.setData({
                bannerUrl: imgbanner
            })
        }
    },
    checkboxChange: function (e) {
      //console.log('checkbox发生change事件，携带value值为：', e);

      var checkboxItems = this.data.keepItems, values = e.detail.value;
      for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
        checkboxItems[i].checked = false;
        for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
          if (checkboxItems[i].value == values[j]) {
            checkboxItems[i].checked = true;
            break;
          }
        }
      }
      //重新计算保养金额
      var kpmoney=0;
      for (var k = 0, lenk = checkboxItems.length; k < lenk; ++k) {
        if (checkboxItems[k].checked){
          kpmoney = parseFloat(kpmoney) + parseFloat(checkboxItems[k].keyval);
        }
      }
      //console.log(kpmoney);
      this.setData({
        keepItems: checkboxItems,
        keepmoney: kpmoney,
        allmoney: parseFloat(kpmoney) + parseFloat(this.data.othermoney)
      });
    },
    getOrderdetail: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'handler/ordkpdetail';
        wx.request({
            url: requrl,
            data: { 'oid': that.data.orderid },
            header: {
                'grant-appid': app.globalData.appid
            },
            success: function (res) {
               // console.log(res);
                if (res.data.code == 200) {
                    var orderinfo = res.data.result;
                    var _isshow = true;
                    if (orderinfo.status != 0) {
                        _isshow = false;
                    }
                    that.setData({
                        orderinfo: orderinfo,
                        keepItems: orderinfo.keepitem,
                        keepmoney: orderinfo.money,
                        othermoney: orderinfo.othermoney,
                        allmoney: parseFloat(orderinfo.money) + parseFloat(orderinfo.othermoney),
                        isshow: _isshow
                    })
                }
            }
        })
    },
    confirmItem: function () {
        var that = this;
        var clickflag = 1;
        var checkboxItems = this.data.keepItems;
        var kpitemArr=[];
        for (var k = 0, lenk = checkboxItems.length; k < lenk; ++k) {
          if (checkboxItems[k].checked) {
            kpitemArr.push(checkboxItems[k].id);
          }
        }
        var kpitem = kpitemArr.join(',');
        var postdata = {
            'openid': wx.getStorageSync('openId'),
            'uid': wx.getStorageSync('userId'),
            'oid': this.data.orderid,
            'kpitem': kpitem
        };
        //console.log(postdata);
        var requrl = app.globalData.hostUrl + 'handler/kpordconfirm';
        if (clickflag == 1) {
            clickflag = 0;
            wx.request({
                url: requrl,
                data: postdata,
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'grant-appid': app.globalData.appid
                },
                method: 'POST',
                success: function (res) {
//                    console.log(res);
                    if (res.data.code == 200) {
                        wx.showModal({
                            title: '提示',
                            content: '提交成功!',
                            success: function (res) {
                                wx.navigateTo({
                                    url: '../keepcar/orderdetail?oid='+that.data.orderid
                                })
                            }
                        });
                    }
                    else {
                        util.isError(res.data.result, that);
                    }
                    clickflag = 1;
                }
            })
        }
    },
  confirmpay: function () {
    var that = this;
    var clickflag = 1;
    var postdata = {
      'openid': wx.getStorageSync('openId'),
      'uid': wx.getStorageSync('userId'),
      'oid': this.data.orderid,
      'flag': 'kp'
    };
    //console.log(postdata);
    var requrl = app.globalData.hostUrl + 'handler/orderPrepay';
    if (clickflag == 1) {
      clickflag = 0;
      wx.request({
        url: requrl,
        data: postdata,
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'grant-appid': app.globalData.appid
        },
        method: 'POST',
        success: function (res) {
          //console.log(res);
          if (res.data.code == 200) {
            wx.requestPayment({
              'timeStamp': res.data.result.timeStamp,
              'nonceStr': res.data.result.nonceStr,
              'package': res.data.result.package,
              'signType': 'MD5',
              'paySign': res.data.result.paySign,
              'success': function (res) {
                wx.showModal({
                  title: '提示',
                  content: '支付成功!',
                  success: function (res) {
                    wx.navigateTo({
                      url: '../myself/myappointment',
                    })
                  }
                });
              },
              'fail': function (res) {

              },
              'complete': function (res) {
                wx.navigateTo({
                  url: '../myself/myappointment',
                })
              }
            });

          }
          else {
            util.isError(res.data.result, that);
          }
          clickflag = 1;
        }
      })
    }
  }

})